// pages/result-manual/index.js
  var app = getApp();
  var md5 = require("../../utils/md5.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
   price:0,
    goods:'',
    good_type:false,
    welfare_type: true,
    welfare_tips:'今日福利',
    shop_quan_id : 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      price: options.price,
      shop_name: options.shop_name,
      shop_id: options.shop_id,
      open_id: options.openid,
      user_id: app.globalData.userId,
      order_number: options.order_number,
    })
    this.getShopRed();
    this.getShopProduct();
    wx.setStorageSync('shop_id', options.shop_id);
  },

  getShopRed: function(){
    var that = this
    var service = 'Red.GetShopRed';
    var shop_id = that.data.shop_id;
    var sign = md5.md5(service + shop_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        shop_id: shop_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info.shop_quan_id > 0){
            that.setData({
              shop_quan_id:result.info.shop_quan_id,
              welfare_type:false,
            })
          }
        } else {
          //没有红包三秒后跳转
          that.getStart();
        }
      },
    })
  },

  getShopProduct: function () {
    var that = this
    var service = 'Shop.GetShopInfo';
    var openid = that.data.open_id;
    var user_id = app.globalData.userId;
    var shop_id = that.data.shop_id;
    var sign = md5.md5(openid + service + shop_id + user_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        openid: openid,
        shop_id: shop_id,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          console.log(result.info.shop_product);
          if (result.info.shop_product.length > 0){
            var goods = [];
            if (6 >result.info.shop_product.length){
              for (var a = 0; a < result.info.shop_product.length; a++) {
                goods.push(result.info.shop_product[a]);
              }  
            }else{
              for (var a = 0; a < 6; a++) {
                goods.push(result.info.shop_product[a]);
              }
            }
            this.setData({
              goods: goods,
            })
          }else{
            this.setData({
              good_type:true,
            })
          }
          
        } else {
          wx.showToast({
            title: '获取失败',
            icon: 'success',
            duration: 2000
          })
        }
      },
    })
  },

  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id
    })
  },

 /**
   * 请求优惠
   */
  receive: function (){
    var that = this
    var service = 'Red.QiangShopRed';
    var openid = that.data.open_id;
    var shop_quan_id = that.data.shop_quan_id;
    var order_num = that.data.order_number;
    var sign = md5.md5(openid + '' + order_num + service + shop_quan_id);
    wx.request({
      url: app.globalData.serviceUrl + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        order_num: order_num,
        shop_quan_id: shop_quan_id,
        openid: openid,
      },
      success: (res) => {
        var result = res.data.data;
        if (result.code == 0) {
          if (result.info.status == 1) {
            that.setData({
              welfare_tips: '成功抢得' + result.info.qiang_money + '元',
            });
            that.getStart();
          }
        } else {
          wx.showModal({
            title: '提示',
            content: result.info.message,
            showCancel: false
          })
        }
      },
    })
  },

  //三秒后执行跳转
  getStart: function () {
    var that = this;
    console.log('跳转');
    setTimeout(function () {
      wx.reLaunch({
        url: "/pages/jingxuan/index?shop_id=" + that.data.shop_id
      })
    }, 3000);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})